package com.wl.cloud.security.register;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.wl.cloud.security.model.LoginUserDetails;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

/**
 * @author: wanglin
 * @date: 2023-11-08 周三
 * @Version: 1.0
 * @Description:
 */
public class LoginUserRegistryImpl implements LoginUserRegistry {
    private final ConcurrentMap<String, LoginUserDetails> loginUsers;

    public LoginUserRegistryImpl() {
        this.loginUsers = new ConcurrentHashMap<>();
    }

    @Override
    public List<LoginUserDetails> getAllLoginUsers() {
        return loginUsers.values().stream().collect(Collectors.toList());
    }

    @Override
    public List<String> getAllUserKeys() {
        List<String> collect = loginUsers.keySet().stream().collect(Collectors.toList());
        return collect;
    }

    @Override
    public LoginUserDetails getLoginUser(String userKey) {
        return StrUtil.isNotBlank(userKey) ? loginUsers.get(userKey) : null;
    }

    @Override
    public void registerNewLoginUser(LoginUserDetails userDetails) {
        if (ObjUtil.isNotNull(userDetails) && StrUtil.isNotBlank(userDetails.getUsername())) {
            loginUsers.put(userDetails.getUsername(), userDetails);
        }
    }

    @Override
    public void registerNewLoginUser(String userKey, LoginUserDetails userDetails) {
        if (StrUtil.isNotBlank(userKey) && ObjUtil.isNotNull(userDetails)) {
            loginUsers.put(userKey, userDetails);
        }
    }

    @Override
    public void removeLoginUser(String userKey) {
        if (StrUtil.isNotBlank(userKey)) {
            loginUsers.remove(userKey);
        }
    }
}
